﻿using MediatR;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.GetUPCData
{
    /// <summary>
    /// 获取商品代号表数据
    /// </summary>
    public class GetUPCDataHandler : IRequestHandler<GetUPCDataCommand, Result>
    {
        public GetUPCDataHandler(IBaseRepositoryGet<UPCData> uPCDataRepositoryGet)
        {
            UPCDataRepositoryGet = uPCDataRepositoryGet;
        }

        public IBaseRepositoryGet<UPCData> UPCDataRepositoryGet { get; set; }
        public async Task<Result> Handle(GetUPCDataCommand request, CancellationToken cancellationToken)
        {
            var result=new Result() { Code=(int)CommonErrorCode.Search};
            var list = UPCDataRepositoryGet.GetAll().ToList();
            if (request.GoodsMinuteId > 0)
            {
                list = list.Where(x => x.GoodsMinuteId == request.GoodsMinuteId).ToList();
            }
            if (!string.IsNullOrEmpty(request.UPCDataName))
            {
                list = list.Where(x => x.UPCDataName.Contains(request.UPCDataName)).ToList();
            }
            if (list != null)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Data = list;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
